//
//  companyProfileVC.m
//  HappyStore
//
//  Created by 陈锋 on 16/6/13.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "companyProfileVC.h"

@interface companyProfileVC ()<UITextViewDelegate>
@property (strong, nonatomic) UILabel *tip;
@property (strong, nonatomic) UILabel *count;
@end

@implementation companyProfileVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = @"企业简介";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    self.tabBarController.tabBar.hidden = YES;
    
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIButton * commentBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44.0f, 44.0f)];
    commentBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [commentBtn setTitle:@"  保存" forState:UIControlStateNormal];
    [commentBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [commentBtn addTarget:self action:@selector(rightBarButtonItemClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightBtn = [[UIBarButtonItem alloc]initWithCustomView:commentBtn];
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    /*创建投诉文本框*/
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 20, screenH - 200)];
    textView.text = @"";
    textView.font = [UIFont fontWithName:@"Arial" size:12.0f];
    textView.backgroundColor = [UIColor whiteColor];
    textView.textAlignment = NSTextAlignmentLeft;
    textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
    textView.returnKeyType = UIReturnKeyDefault;
    textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    textView.layer.borderWidth = 1.0;
    textView.layer.cornerRadius = 8.0f;
    textView.layer.masksToBounds = YES;
    textView.scrollEnabled = YES;
    textView.delegate = self;
    self.textView = textView;

    /*自定义文本框placeholder*/
    UILabel * tip = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width - 20, 25)];
    if ([self.textStr isEqualToString:@""]||[self.textStr isEqualToString:@"null"]||self.textStr == nil) {
        tip.text = @"请输入企业简介";
    }else{
        self.textView.text = self.textStr;
    }
    tip.font = [UIFont fontWithName:@"Arial" size:12.0f];
    tip.backgroundColor = [UIColor clearColor];
    tip.enabled = NO;
    
    self.tip = tip;
    
    /*自定义文本框字数统计*/
    UILabel * count = [[UILabel alloc]initWithFrame:CGRectMake(screenW - 80,self.textView.height - 30, 80, 30)];
    count.text = @"0/500";
    count.textAlignment = NSTextAlignmentCenter;
    count.font = [UIFont fontWithName:@"Arial" size:12.0f];
    count.backgroundColor = [UIColor clearColor];
    count.enabled = NO;
    self.count = count;
    
    [self.view addSubview:self.textView];
    [self.textView addSubview:self.tip];
    [self.textView addSubview:self.count];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}


-(void)leftBarButtonItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)rightBarButtonItemClick{
    
    if (self.textView.text.length > 1) {
        [self showRoundProgressWithTitle:@"请稍后..."];
        
        NSMutableDictionary *muDicParameters = [NSMutableDictionary dictionary];
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"userid"] forKey:@"userId"];
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"token"] forKey:@"token"];//验证用户
        [muDicParameters setObject:self.textView.text forKey:@"introduction"];//资源类型
        
        NSString *strURl;
        
        strURl = [NSString stringWithFormat:@"%@%@",URL_DEFINE,@"updateUser_Company_app_myHome.im.lensum"];
        
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager POST:strURl parameters:muDicParameters progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
            //        NSLog(@"%@",dicInfo);

            if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
                
                [self.navigationController popToRootViewControllerAnimated:YES];
                
                [self showRightWithTitle:[dicInfo objectForKey:@"msg"] autoCloseTime:2];
                
            }else {
                
                [self showSingleIconBubble:[dicInfo objectForKey:@"msg"]];
            }
            

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

            [self showErrorWithTitle:@"1" autoCloseTime:2];
            
        }];
    }else{

        [self showSingleIconBubble:@"内容不能为空"];
    }


    

}
// 自定义文本框placeholder

- (void)textViewDidChange:(UITextView *)textView {
    textView = self.textView;
    NSInteger number = [textView.text length];
    if (number >= 500) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"字符个数不能大于500" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        textView.text = [textView.text substringToIndex:500];
        number = 500;
        
    }
    _count.text = [NSString stringWithFormat:@"%ld/500",(long)number];
    
    if (textView.text.length > 0) {
        _tip.text = @"";
        
    }else{
        _tip.text = @"输入企业简介";
    }
}
@end
